package com.gxkj.projects.myshopx.dao;

import com.gxkj.common.hibernate.HibernateBaseRepositoryImpl;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.entitys.Attribute;
import com.gxkj.projects.myshopx.entitys.Brand;
import com.gxkj.projects.myshopx.enums.AttributeStates;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AttributeDaoImpl extends HibernateBaseRepositoryImpl {

    public ListPager<Attribute> doPageHQL(int pagenNo, int pageSize, Attribute attribute){
        ListPager<Attribute> pager = new ListPager<Attribute>();
        pager.setPageNo(pagenNo);
        pager.setRowsPerPage(pageSize);
        String hql  = " from Attribute a ";
        Map<String,Object> param = new HashMap<String,Object>();
        if(attribute==null || attribute.getStates() == null   ){
            hql += " where states != :states ";
            param.put("states", AttributeStates.DEL);

        }else {
            hql += " where states = :states ";
            param.put("states",attribute.getStates());
        }
        if(attribute != null){
            if(StringUtils.isNotBlank(attribute.getTypeId())){
                hql += " and typeId = :typeId ";
                param.put("typeId",attribute.getTypeId());
            }
            if(StringUtils.isNotBlank(attribute.getAttrName())){
                hql += " and attrName like :attrName ";
                param.put("attrName","%"+attribute.getAttrName()+"%");
            }
        }
        return  this.selectPageByHql(hql,param,pager);
    }

    public boolean isExitByName(String id,String name,String typeId){
        String hql = null;
        Map<String,Object> param = new HashMap<String,Object>();
        if(StringUtils.isBlank(id)){
            hql = " from Attribute a where states != :states and attrName=:name and typeId = :typeId";
            param.put("states", AttributeStates.DEL);
            param.put("name",name);
            param.put("typeId",typeId);
        }else {
            hql = " from Attribute a where states != :states and attrName=:name and id !=:id  and typeId = :typeId";
            param.put("states", AttributeStates.DEL);
            param.put("name",name);
            param.put("id",id);
            param.put("typeId",typeId);
        }
        Attribute attribute = this.selectFirstOneByHQL(hql,param);
        if(attribute == null){
            return false;
        }else {
            return true;
        }

    }

    /**
     * 按照类型查询可用的属性
     * @param goodsTypeId
     * @return
     */
    public  List<Attribute> getAttributeListByTypeId(String goodsTypeId){
        String hql = "from Attribute a where states = :states and typeId = :typeId";

        Map<String,Object> param = new HashMap<String,Object>();
        param.put("states", AttributeStates.NORMAL);
        param.put("typeId", goodsTypeId);
        return this.selectListByHQL(hql,param);
    }
}
